<template>
  <view class="content">
    <view class="warp">
      <view class="flex">
        {{ $t('login.address') }}：
        <u--input
          :placeholder="$t('login.placeholder')"
          border="surround"
          v-model="urlEnv"
          style="margin: 0 12rpx"
          clearable
        ></u--input>
        <image
          src="@/static/icon/scan.png"
          @click="scanCodeFun"
          style="width: 40rpx; height: 40rpx"
        ></image>
      </view>
      <view class="tips">{{ $t('login.example') }}：http://beta-tms-hq.havi-cn.com</view>
      <u-button
        type="primary"
        style="margin-top: 100rpx; width: 280rpx"
        @click="goLogin"
        :disabled="!urlEnv"
        :text="$t('btn.confirm')"
      ></u-button>
    </view>
  </view>
</template>

<script>
import { mapGetters } from 'vuex';
import { getAppEnv } from '@/api/config';
export default {
  data() {
    return {
      urlEnv: '',
      ercode: '',
    };
  },
  computed: {
    ...mapGetters(['envUrl']),
  },
  onBackPress() {
    if (!this.urlEnv) {
      // 没输入禁止返回
      return true;
    }
    return false;
  },
  onLoad() {
    if (this.envUrl) {
      this.urlEnv = this.envUrl.value;
    }
  },
  methods: {
    // 扫描
    scanCodeFun() {
      const that = this;
      uni.scanCode({
        success: function (res) {
          that.urlEnv = res.result;
        },
      });
    },
    goLogin() {
      var reg = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
      if (!this.urlEnv) {
        uni.showToast({
          title: '请输入/扫描地址',
          icon: 'none',
          duration: 1500,
        });
      } else if (!reg.test(this.urlEnv)) {
        uni.showToast({
          title: '不是合法域名',
          icon: 'none',
          duration: 1500,
        });
      } else {
        this.$store.dispatch('changeEnv', {
          label: '系统环境',
          value: this.urlEnv,
        });
        setTimeout(() => {
          getAppEnv().then((res) => {
            if (res.code == 0) {
              uni.navigateTo({
                url: '/pages/login/index',
              });
            } else {
              uni.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1500,
              });
            }
          });
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  background: #fff;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 24rpx;
  .warp {
    width: 94%;
  }
  .flex {
    display: flex;
    align-items: center;
  }
  .tips {
    font-size: 24rpx;
    color: #ccc;
    margin-top: 20rpx;
    padding-left: 68rpx;
  }
}
</style>
